#!/usr/bin/env python
    ##########################################################################
    ##(c) 2008 Bastian Damman                                               ##
    ##                                                                      ##
    ##This file is part of Kaboom-O-rama.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is free software: you can redistribute it and/or modify ##
    ##it under the terms of the GNU General Public License as published by  ##
    ##the Free Software Foundation, either version 3 of the License, or     ##
    ##(at your option) any later version.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is distributed in the hope that it will be useful,      ##
    ##but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
    ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
    ##GNU General Public License for more details.                          ##
    ##                                                                      ##
    ##You should have received a copy of the GNU General Public License     ##
    ##along with Kaboom-O-rama.  If not, see <http://www.gnu.org/licenses/>.##
    ##########################################################################
from pygame import *
import math

KEYWAITFIRST = 30
KEYWAITSECOND = 2

class Tool:
    def __init__(self, parent):
        self.parent = parent
        self.grid = self.parent.grid
        self.current_tool = 0
        self.tools = []
        self.tool_flags = []
        self.active = True
        self.mb_left, self.mb_right = False, False
        self.keys = [False, False, False, False]
        self.keys_wait = [-1, -1, -1, -1]
        
        self.img = None
        
        self.add_tool(ToolRemove(self, self.grid))
        self.add_tool(ToolGround(self, self.grid))
        
        for t in self.tools:
            t.post_init()
            
        self.set_tool(1)
    
    def add_tool(self, tool):
        tool.set_type(len(self.tools))
        self.tools.append(tool)
    
    def set_active(self, yes):
        self.active = yes
    
    def set_tool(self, tool):
        self.current_tool = tool
        #self.img = self.tools[self.current_tool].get_img()
    
    def get_tool_flags(self, tool):
        return self.tool_flags[tool]
    
    def keyboard_handle(self, events):
        for i in range(0, 4):       
            if self.keys_wait[i] > 0:
                self.keys_wait[i] -= 1
                self.keys[i] = False
            elif self.keys_wait[i] == 0:
                self.keys_wait[i] = KEYWAITSECOND
                self.keys[i] = True               
        
        keys = [None, None, None, None]
        for e in events:
            if e.type == KEYDOWN:
                if e.key == K_LEFT:
                    keys[0] = True
                elif e.key == K_RIGHT:
                    keys[1] = True
                elif e.key == K_UP:
                    keys[2] = True
                elif e.key == K_DOWN:
                    keys[3] = True
            elif e.type == KEYUP:
                if e.key == K_LEFT:
                    keys[0] = False
                elif e.key == K_RIGHT:
                    keys[1] = False
                elif e.key == K_UP:
                    keys[2] = False
                elif e.key == K_DOWN:
                    keys[3] = False
        for i in range(0,4):
            if keys[i] == True:
                self.keys[i] = True
                self.keys_wait[i] = KEYWAITFIRST
            elif keys[i] == False:
                self.keys[i] = False
                self.keys_wait[i] = -1
                
        for k in range(0, 4):
            if not self.keys[k]:
                continue
            #if k == 0: self.tools[self.current_tool].keyboard_input(K_LEFT)
            #elif k == 1: self.tools[self.current_tool].keyboard_input(K_RIGHT)
            #elif k == 2: self.tools[self.current_tool].keyboard_input(K_UP)
            #elif k == 3: self.tools[self.current_tool].keyboard_input(K_DOWN)
    
    def update(self, grid, events):
        self.keyboard_handle(events)
        for e in events:
            if e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    self.mb_left = True
                elif e.button == 3:
                    self.mb_right = True
            elif e.type == MOUSEBUTTONUP:
                if e.button == 1:
                    self.mb_left = False
                elif e.button == 3:
                    self.mb_right = False
        if self.active:
            mousepos = mouse.get_pos()
            self.pos = grid.snap(mousepos)
            self.pos =(self.pos[0] + self.parent.camera.get_x() + self.grid.cel_size,
                    self.pos[1] + self.parent.camera.get_y() + self.grid.cel_size)
            if self.mb_left:
                self.tools[self.current_tool].use(self.pos, self.grid)
            elif self.mb_right:
                self.tools[self.current_tool].use2(self.pos, self.grid)
                
    def draw(self, camera):
        self.tools[self.current_tool].draw(self.pos, camera)
    
class ToolItem:
    def __init__(self, img):
        self.init(img)
    
    def init(self, parent, grid, default_flags):
        self.parent = parent
        self.init_flags(default_flags)
        self.cel_size = grid.cel_size
    
    def init_flags(self, default_flags):
        self.default_flags = default_flags
        self.parent.tool_flags.append(self.default_flags)

    def post_init(self):
        pass
    
    def set_type(self, type):
        self.type = type #aka: id
    
    def keyboard_input(self, key):
        pass
           
    def use(self, pos, grid):
        grid.add(pos, self)
    
    def use2(self, pos, grid):
        grid.clear(self.pos[0], self.pos[1], self.cel_size, self.cel_size, self.type)
        
    def snap(self, pos):
        x = int((pos[0] / self.cel_size) * self.cel_size)
        y = int((pos[1] / self.cel_size) * self.cel_size)
        return (x, y)
    
    def get_new_item(self, grid_item): #inits a new grid_item
        pass
    
    def draw(self, pos, camera):
        pass

class ToolGround(ToolItem):
    def __init__(self, parent, grid):
        self.init(parent, grid, [grid.cel_size * 4, grid.cel_size * 4, 0])
                               #[w, h, scpack]
        self.img = [Surface((self.cel_size, self.cel_size))]
        self.img[0].fill((200, 150, 0))
        self.img[0].set_alpha(200)
        
        
        
    def get_new_item(self, grid_item): #inits a new grid_item
        flags = self.parent.get_tool_flags(self.type)
        grid_item.scPack = flags[2]
        grid_item.img = self.parent.parent.ic.spr_tool_ground[flags[2]]
        return grid_item
    
    def keyboard_input(self, key):
        flags = self.parent.get_tool_flags(self.type)
        if key == K_LEFT: flags[0] -= self.cel_size
        elif key == K_RIGHT: flags[0] += self.cel_size
        elif key == K_UP: flags[1] += self.cel_size
        elif key == K_DOWN: flags[1] -= self.cel_size
        if flags[0] < self.cel_size: flags[0] = self.cel_size
        if flags[1] < self.cel_size: flags[1] = self.cel_size
    
    def use(self, pos, grid):
        flags = self.parent.get_tool_flags(self.type)
        pos = (pos[0] - flags[0] / 2, pos[1] - flags[1] / 2)
        pos = self.snap(pos)
        for xxx in range(0, flags[0], self.cel_size):
            for yyy in range(0, flags[1], self.cel_size):
                p = (pos[0] + xxx, pos[1] + yyy)
                grid.add(p, self)
                
    def use2(self, pos, grid):
        flags = self.parent.get_tool_flags(self.type)
        pos = self.snap((pos[0] - flags[0] / 2, pos[1] - flags[1] / 2))
        grid.clear(pos[0], pos[1], flags[0], flags[1], self.type)
    
    def draw(self, pos, camera):
        flags = self.parent.get_tool_flags(self.type)        
        pos = (pos[0] - flags[0] / 2, pos[1] - flags[1] / 2)
        pos = self.snap(pos)
        
        for xxx in range(0, flags[0], self.cel_size):
            for yyy in range(0, flags[1], self.cel_size):
                p = (pos[0] + xxx, pos[1] + yyy)
                camera.blit(self.img[flags[2]], p)
        
        
class ToolRemove(ToolItem):
    def __init__(self, parent, grid):        
        self.width, self.height = grid.cel_size * 4, grid.cel_size * 4
        self.init(parent, grid, [self.width, self.height, 0])
        
    def post_init(self):
        self.make_img()
    
    def make_img(self):
        flags = self.parent.get_tool_flags(self.type)
        self.img = Surface((flags[0], flags[1]), SRCALPHA)
        xline_off = 16
        m = 1       
        for x in range(xline_off, flags[0] + flags[1], xline_off):
            x1, x2 = x, 0
            y1 = 0
            y2 = m*(x1 - x2) + y1
            draw.line(self.img, (255, 255, 255), (x1, y1), (x2, y2), 1)
        draw.rect(self.img, (255, 255, 255), (0, 0, flags[0], flags[1]), 1)
    
    def use(self, pos, grid):
        flags = self.parent.get_tool_flags(self.type)
        pos = self.get_pos(pos)
        grid.clear(pos[0], pos[1], flags[0], flags[1])
        
    def use2(self, pos, grid):
        changed = False
        #TODO: show box to define size
        if changed:
            self.make_img()
            
    def get_pos(self, pos):
        flags = self.parent.get_tool_flags(self.type)
        pos = (pos[0] - flags[0] / 2 + self.cel_size / 2, pos[1] - flags[1] / 2 + self.cel_size / 2)
        pos = self.snap(pos)
        return pos
    
    def draw(self, pos, camera):
        flags = self.parent.get_tool_flags(self.type) 
        if self.width != flags[2] or self.height != flags[3]:
            self.width, self.height = flags[2], flags[3]
            self.make_img()
        pos = self.get_pos(pos)
        camera.blit(self.img, pos)